﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DBA.Common;
using DBA.Bll;
using DBA.Data;
using DBA.Data.ADO;
using DBA.Web.UI;
using DBA.Model;

namespace DBA.Web
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack) 
            {
                txtUserName.Focus();
            }
        }

        DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
        protected void btn_submit_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (String.Compare(Session["CheckCode"].ToString(), txtCheckCode.Value.Trim(), true) != 0)
                {
                    JsHelper.Alert(this,"验证码错误，请输入正确的验证码！");
                    return;
                }
            }
            catch
            {
                JsHelper.Alert(this, "登录错误!");
                return;
            }            
            string userName = txtUserName.Value.Trim();
            string passWord = txtPwd.Value.Trim();
            IList<AdoCondition> dict = new List<AdoCondition>();
            dict.Add(new AdoCondition(CompareOper.equal, LogicOper.and, "Name",userName));
            dict.Add(new AdoCondition(CompareOper.equal, LogicOper.and, "Password",passWord));
            

            int state = dal.ModifyData.GetMaxCount<EmployeeInfo>(dict);
            if (state == 1)
            {
                BLLOperatingUser bllOperUser = new BLLOperatingUser();
                //获取操作员的基本信息
                DataTable dt = bllOperUser.GetOperatingUser(userName, passWord);
                OperatingUser loginUser = new OperatingUser();
                loginUser.Dpt_id = ConvertHelper.ToInt32(dt.Rows[0]["dpt_id"]);
                loginUser.DptName = dt.Rows[0]["DptName"].ToString();
                loginUser.Suo_ID = ConvertHelper.ToInt32(dt.Rows[0]["Suo_ID"]);
                loginUser.SuoName = dt.Rows[0]["SuoName"].ToString();
                loginUser.EmployeeID = ConvertHelper.ToInt32(dt.Rows[0]["EmployeeID"]);
                loginUser.UserName = dt.Rows[0]["UserName"].ToString();
                loginUser.Xi_ID = ConvertHelper.ToInt32(dt.Rows[0]["Xi_ID"]);
                loginUser.XiName = dt.Rows[0]["XiName"].ToString();
                loginUser.EaID = ConvertHelper.ToInt32(dt.Rows[0]["EaID"]);
                loginUser.EduName = dt.Rows[0]["EduName"].ToString();
                loginUser.jszwzgID = ConvertHelper.ToInt32(dt.Rows[0]["jszwzgID"]);
                loginUser.JszwzgName = dt.Rows[0]["JszwzgName"].ToString();
                loginUser.PD_id = ConvertHelper.ToInt32(dt.Rows[0]["PD_id"]);
                loginUser.PartyDutyName = dt.Rows[0]["PartyDutyName"].ToString();
                loginUser.SalaryID = dt.Rows[0]["SalaryID"].ToString();
                loginUser.Sex = ConvertHelper.ToBoolean(dt.Rows[0]["Sex"]) ? "男" : "女";

                //获取角色数组
                DataTable dtUserRole = bllOperUser.GetUserRole(loginUser.EmployeeID);
                List<int> roleIDList = new List<int>();
                List<string> roleNameList = new List<string>();
                for (int i = 0; i < dtUserRole.Rows.Count; i++)
                {
                    roleIDList.Add(ConvertHelper.ToInt32(dtUserRole.Rows[i][0]));
                    roleNameList.Add(dtUserRole.Rows[i][1].ToString());
                }
                loginUser.RoleID = roleIDList;
                loginUser.RoleName = roleNameList;
                loginUser.UserFuncList = bllOperUser.GetUserFunction(loginUser.RoleID);

                ServerContext.AdminCurrentUser = loginUser;
                Response.Redirect("default.aspx");
            }
            else
            {
                JsHelper.Alert(this, "登录失败,请检查你的用户名和密码!");
            }

        }

        protected void btn_clear_Click(object sender, ImageClickEventArgs e)
        {
            txtUserName.Value = string.Empty;
            txtPwd.Value = string.Empty;
        }
    }
}